/**
 * Created by lixinkun on 2017/5/7.
 */

import * as TYPES from './actionTypes';
import fetchUrl from '../constants/fetchUrl';
import fetchWithTimeout from '../utils/fetchWithTimeout';
import Toast from 'react-native-root-toast';
import px2dp from '../utils/px2dp';

//获取数据
function requestData() {
    return {
        type: TYPES.FETCH_CATEGORY_DATA_REQUEST
    }
}

function requestMoreData() {
    return {
        type: TYPES.FETCH_CATEGORY_MORE_DATA_REQUEST
    }
}

function receiveData(json) {
    return {
        type: TYPES.FETCH_CATEGORY_DATA_SUCCESS,
        dataSource: json.results
    }
}

function fetchFailure() {
    return {
        type: TYPES.FETCH_CATEGORY_DATA_FAILURE
    }
}

function fetchMoreDataFailure() {
    return {
        type: TYPES.FETCH_CATEGORY_MORE_DATA_FAILURE
    }
}

function fetchFullData() {
    return {
        type: TYPES.FETCH_CATEGORY_DATA_IS_FULL
    }
}

//判断返回数据是否符合格式
function isValidData(responseData) {
    if (responseData.results.length > 0) {
        return true;
    }
    return false;
}

export function fetchData(category) {
    //使用闭包捕获内容
    const url = fetchUrl.category + category;
    return function (dispatch) {
        dispatch(requestData());
        setTimeout(() => {
            fetchWithTimeout(5000, fetch(url))
                .then(response => response.json())
                .then(json => {
                    if (isValidData(json)) {
                        dispatch(receiveData(json));
                    } else {
                        dispatch(fetchFailure());
                    }
                }).catch((error) => {
                console.log(error);
                dispatch(fetchFailure());
            });
        }, 500);
    }
}

export function fetchMoreData(category) {
    const url = fetchUrl.category + category;
    return (dispatch) => {
        dispatch(requestMoreData());
        setTimeout(() => {
            fetchWithTimeout(5000, fetch(url))
                .then(response => response.json())
                .then(json => {
                    if (isValidData(json)) {
                        dispatch(json);
                    } else {
                        dispatch(fetchMoreDataFailure());
                    }
                }).catch((error) => {
                console.log(error);
                Toast.show('获取数据失败', {position: px2dp(-80)});
                dispatch(fetchMoreDataFailure());
            }, 500);
        })
    }
}



